<?php
/**
 * 获取
 *
 *
 */

use Workerman\Worker;
use Workerman\Mysql;

class back_poster_contents{

    public function entry($param){
        //var_dump($param);
        return $this->do_back_poster_contents($param);
    }


    /**
     * do_set_poster_contents
     *
     *
     * @access private
     * @param $param
     * @since 1.0
     * @return array(errcode,errmsg,rtv)
     */
    private function do_back_poster_contents($param){

        if(!empty($param["user_id"])){
            $user_id = $param["user_id"];
        }else{
            $userObj= Token::getToken($_SERVER['HTTP_ACCESS_TOKEN']);
            $user_id = $userObj[0]['id'];
        }

        $sql = "SELECT b.class FROM hf_mes_user a 
				INNER JOIN hf_mes_groups b ON a.groups=b.id 
				INNER JOIN hf_mes_department c ON a.department=c.id
				WHERE a.id='$user_id'";
        $dp = sql_query($sql);

        $group = $dp[0]["class"];
        // group为1、2、3,分别代表采购员、副总监、总监

        // 部门点击提交稿件按钮
        $poster_picture_department_id = $param["poster_picture_department_id"];

        //    $sql_str = " AND c.id='$user_id'";

        if($group == "副总监"){
            //$sql_str = "d.finish=1 AND d.deputy_director='-1'";

            $sql = "update hf_mes_poster_picture_department set finish=0,deputy_director=-1 WHERE id='$poster_picture_department_id'";

        }elseif($group == "总监"){
            //$sql_str = "d.chief_inspector='-1' AND d.deputy_director!='-1'";
            $sql = "update hf_mes_poster_picture_department set deputy_director=-1,deputy_director=-1 WHERE id='$poster_picture_department_id'";
            //$is_chief_inspector = 1;
        }

            $rt = sql_query($sql);
           // $this->update_history_contents($group,$sql_str);

        return array("errcode"=>0, "errmsg"=>"退稿成功", "rtval"=>"");

    }

    /**
     * check_poster_finish
     * 判断该部门下稿件都是否校稿完成
     *
     * @access private
     * @param $poster_contents_id,$sql_str
     * @since 1.0
     * @return Boolean
     */
    private function check_poster_finish($ret){

        $finish = 1;
        foreach($ret as $values){
            if($values["contents"] == Null || $values["contents"] == "" || count(json_decode($values["contents"])) == 0){
                $finish = 0;
            }
        }
        return $finish;
    }

    /**
     * get_picture_department_id
     * 获取picture_department_id
     *
     * @access private
     * @param $ret
     * @since 1.0
     * @return string
     */
    private function get_picture_department_id($ret){

        $sql_str =  "";
        foreach($ret as $values){
            $sql_str .= $values["id"].",";
        }
        $sql_str = rtrim($sql_str,",");
        return $sql_str;
    }

    /**
     *   更新历史修改内容数据
     *
     * 1、如果是采购员，就更新内容到user_contents
     * 2、如果是副总监，就更新内容到deputy_director_contents
     *
     * @access private
     * @param $ret
     * @since 1.0
     * @return string
     */
    private function update_history_contents($group,$sql_str){

        if($group != "总监"){
            $picture_department_ids = explode(",",$sql_str);
            foreach($picture_department_ids as $picture_department_id){
                $sql = "SELECT contents FROM hf_mes_poster_picture_department WHERE id='$picture_department_id'";
                $ret = sql_query($sql);
                $history_contents = $ret[0]["contents"];
                $sql = "update hf_mes_poster_picture_department set user_contents='$history_contents' WHERE id='$picture_department_id'";
                if($group == "副总监"){
                    $sql = "update hf_mes_poster_picture_department set deputy_director_contents='$history_contents' WHERE id='$picture_department_id'";
                }
                sql_query($sql);

            }


        }




    }


    /**
     * create
     * 通过 call_user_func 获得本class的实例
     *
     * @access public
     * @since 1.0
     * @return object 本类的实例
     */
    public static function create(){
        static $instance;
        if (!$instance){
            $instance = new back_poster_contents();
        }
        return $instance;
    }
}


?>